Recursion

Recursive Sequences

Sequence: An ordered list.

Recursive Sequence: Sequence where later values are defined based on earlier values.

Example: Geometric Progression

Example: Fibonacci

Recursively-Defined Operations

Exponential Operation:

Multiplication Operation:

Example: Find a recursive definition for factorial (n!)

Implementing Recursive Algorithms

Once you figure out a recursive definition for a function, you can immediately turn it into a recursive algorithm in a programming language, like so:

public static int factorial (int n) {
    if (n==0)
        return 1;
    else
        return n*factorial(n-1);
}

In general, starting a recursive function— f(n) = \begin{cases} &\text{output}_1 \text{, if $n$ in } \text{range}_1 \\ &\vdots \\ &\text{output}_k \text{, if $n$ in } \text{range}_k \end{cases}

—gives a recursive algorithm:

Function f(n) {
    if (n in range_1)
        return output_1
    ...
    if (n in range_k)
        return output
}

Recurrence Relations

Recurrence Relation: The equation that defines the later values in a recursive sequence based on the preceding values.

Example:

Properties of Recurrence Relations

Linear Recurrence Relation: Recurrence relation where earlier values have a power of 1.

Nonlinear Recurrence Relation: Recurrence relation where earlier values have powers other than 1.

Homogeneous Recurrence Relation: Recurrence relation that has nothing but proceeding terms.

Inhomogeneous Recurrence Relation:

Constant Coefficients

A recurrence relation is said to have constant coefficients if the coefficients before the proceeding terms are all constants.

Example: The Fibonacci relation is homogeneous, linear, and has constant coefficients:

Example: This recurrence relation doesn’t have constant coefficients:

Order of Recurrence Relation

Order of a relation is defined by number of previous terms in a relation for the nth term.

Note: Degree one, degree two, and degree three can also be referred to as “first order”, “second order”, and third order”, respectively.

Solving Recurrence Relations

Professor’s Note: In real-life, explicit formulas tend to outperform iterative solutions, essentially:

“Solving a Recurrence Relation”: The process of converting a recursive definition to an explicit formula.

Introduction

Example: Interest as a Recurrence Relation v.s. Explicit Form:

Suppose you have $1000 in a 5% APY savings account.

You could calculate your total savings as a recurrence relation like so:

S(0) = \$1000 \\ S(n) = S(n-1) \times 1.05

Or, you could use the simpler equivalent explicit form: 1000(1.05)^n

The Characteristic Root Technique

The only recurrence relations we can systematically convert to explicit form are recurrence relations of the form: a_n = c_1 a_{n-1} + c_2 a_{n-2} + ... + c_k a_{n-k} \\

Characteristics of recurrence relations that can be systematically solved:

When solving these kinds of recurrence relations, our goal is to get a solution of the form: a_n = r^n

To solve these recurrence relations we use the characteristic equation: r^k - c_1 r^k-1 - c_2 r^{k-2} - ... - c_k = 0

Note: Deriving the Characteristic Equation \begin{aligned} a_n &= c_1 a_{n-1} + c_2 a_{n-2} + ... + c_k a_{n-k} \\ r_n &= c_1 r^{n-1} + c_2 r^{n-2} + ... + c_k r^{n-k} \text{ (replace $a_n$ with $r^n$)} \\ r^k &= c_1 r^k-1 + c_2 r^{k-2} + ... + c_k \text{ (divide by $r^{n-k}$)} \\ &\text{ (Then just move everything to one side)} \end{aligned}

Theorem: Solving Relations of Degree Two

Theorem: Solving Linear Homogeneous Recurrence Relations of Degree Two


Example: Find the solution of the recurrence relation:

  1. Getting the characteristic equation from a_n:
  1. Solve characteristic equation (finding roots):

\begin{aligned} r^2 - r - 2 &= 0 \\ (r-2) (r+1) &= 0 \\ &\therefore r_1 = 2 \land r_2 = -1 \end{aligned}

  1. Plug the roots into a_n:

a_n = \alpha_1 2^n + \alpha_2 (-1)^n

  1. Find \alpha_1 and \alpha_2 by plugging a_0 and a_1 into a_n:

a_0 = 2 = \alpha_1 + \alpha_2 \\ a_1 = 7 = 2\alpha_1 - \alpha_2

  1. Answer: Plug \alpha’s back into a_n

a_n = 3 (2^n) - ( - 1 ) ^ n